package com.csj.mvvmtest.ui.second

import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.csj.mvvm.ui.base.BaseFragment
import com.csj.mvvmtest.bean.NaviBean
import com.csj.mvvmtest.databinding.FragmentSecondBinding

class SecondFragment : BaseFragment<NaviViewModel, FragmentSecondBinding>() {
    private lateinit var leftAdapter: SecondLeftAdapter
    private var rightAdapter: SecondRightAdapter? = null
    private var leftList: ArrayList<NaviBean> = ArrayList()
    private var rightList: ArrayList<NaviBean.ArticlesBean> = ArrayList()

    override fun initVM() {
        vm.articlesData.observe(this, Observer {
            leftAdapter!!.replaceData(it.data)
            if (it.data.isNotEmpty()) {
                rightAdapter!!.replaceData(
                    leftList[0].articles
                )
            }
        })
    }

    override fun initClick() {
    }

    override fun initData() {
        leftAdapter = SecondLeftAdapter(mContext, leftList)
        rightAdapter = SecondRightAdapter(mContext, rightList)
        v.left.layoutManager = LinearLayoutManager(mContext)
        v.left.adapter = leftAdapter
        v.right.layoutManager = LinearLayoutManager(mContext)
        v.right.adapter = rightAdapter
        leftAdapter.itemClick { view, i ->
            leftAdapter.selPos = i
            leftAdapter.notifyDataSetChanged()
            rightAdapter!!.replaceData(
                leftList[i].articles
            )
        }
    }

    override fun lazyLoadData() {
        vm.getData()
    }
}